package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsMCulture;
import com.wenwu.common.bean.RelicsMRepair;
import com.wenwu.common.bean.RelicsMRepairExample;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsMRepairMapper;
import com.wenwu.service.MoveableRepairService;

@Service
public class MoveableRepairServiceImpl implements MoveableRepairService {

	@Autowired
	private RelicsMRepairMapper relicsMRepairMapper;
	
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	
	public RelicsMRepair selectMRepairByMWenwuKey(Integer key)
			throws RelicsException {
		try {
			RelicsMRepairExample example = new RelicsMRepairExample();
			example.createCriteria().andMRelicsIdEqualTo(key);
			List< RelicsMRepair> list = relicsMRepairMapper.selectByExample(example);
			if(list == null || list.size() <=0){
				return null;
			}else{
				return list.get(0);
			}
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public String addRelicsMRepair(RelicsMRepair relicsMRepair)
			throws RelicsException {
		try{
			String result = null;
			int num = 0 ;
			List<RelicsAttachment> list = relicsMRepair.getFileList();
			if (relicsMRepair.getIntegrityId() == null) {
				num += relicsMRepairMapper.insertSelective(relicsMRepair);
				if (relicsMRepair.getIntegrityId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("可移动文物-完成情况附件");
							attachment.setTableOwner(relicsMRepair.getIntegrityId());// 外键
						}
						num += attachmentMapper.insertList(list);
					}
				}
				if (num > 0) {
					result = "可移动文物-完成情况添加成功";
				} else {
					result = "可移动文物-完成情况添加失败";
				}
			} else {
				num += attachmentMapper.deleteByKey(relicsMRepair.getIntegrityId(),relicsMRepair.getOwnList());//先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("可移动文物-完成情况附件");
						attachment.setTableOwner(relicsMRepair.getIntegrityId());// 外键
					}
					num += attachmentMapper.insertList(list);
				}
				num += relicsMRepairMapper.updateByPrimaryKeySelective(relicsMRepair);
				if (num > 0) {
					result = "可移动文物-完成情况修改成功";
				} else {
					result = "可移动文物-完成情况修改失败";
				}
			}
			return result;
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public RelicsMRepair selectMRepairBySelftKey(Integer integrityId) throws RelicsException{
		try{
			return relicsMRepairMapper.selectByPrimaryKey(integrityId);
		}catch(Exception e){
			throw new RelicsException("系统错误，请稍后再试",e);
		}
	}

	public String deleteBykeys(String keys) throws RelicsException{
		String result = "删除失败！";
		if (keys != null) {
			try {
				List<String> keyList = new ArrayList<String>();
				String[] objs = keys.split(",");
				for (String k : objs) {
					keyList.add(k);
				}
				result = "成功删除" + relicsMRepairMapper.deleteInfoByKey(keyList) + "条数据";
			} catch (Exception e) {
				throw new RelicsException("系统错误，请稍后再试", e);
			}
		}
		return result;
	}

	public PaginationVO selectByCondition(RelicsMRepair relicsMRepair,
			PaginationVO pageVo) throws RelicsException {
		try {
			List<RelicsMCulture> list = relicsMRepairMapper.selectListByPage(relicsMRepair, pageVo);
			int count = relicsMRepairMapper.selectTotalRecord(relicsMRepair);
			pageVo.setList(list);
			pageVo.setTotalRecord(count);
			return pageVo;
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

}
